package simmar.xcs;

import java.util.Random;

import ch.idsia.benchmark.tasks.SystemOfValues;
import ch.idsia.tools.MarioAIOptions;
import simmar.utilities.IO;
import simmar.utilities.Logger;
import simmar.xcs.XCSConfig;
import simmar.xcs.XCSConfig.MODE;

public class XCSMain
{
    public static void main(String[] args)
    {
        Random randomNumber = new Random(System.currentTimeMillis());

        String[] xcsArgs = { "-ag", "simmar.xcs.XCSAgent" };
        final MarioAIOptions options = new MarioAIOptions(xcsArgs);
        options.setLevelDifficulty(0);

        // Set up system of values
        SystemOfValues sov = new SystemOfValues();

        sov.coins = 0;
        sov.distance = 2;
        sov.flowerFire = 0;
        sov.greenMushroom = 0;
        sov.hiddenBlock = 0;
        sov.killedByFire = 4;
        sov.killedByShell = 0;
        sov.killedByStomp = 1;
        sov.kills = 2;
        sov.mode = 32;
        sov.mushroom = 0;
        sov.stomp = 0;
        sov.timeLeft = 4;
        sov.win = 512;

        Logger.logLine("XCSAgent learning phase started.");
        options.setVisualization(false);

        final XCSLearningTask learningTask = new XCSLearningTask(options);

        int fitness = -1;
        int difficulty = -1;
        int count = 0;

        while (fitness != 0)
        {
            int nextSeed = randomNumber.nextInt(Integer.MAX_VALUE);
            difficulty = count % 2;
            // Logger.logLine("Running evaluation with difficulty " + difficulty
            // + " and seed " + nextSeed);
            options.setLevelRandSeed(nextSeed);
            options.setLevelDifficulty(difficulty);
            fitness = learningTask.evaluate(sov);
            // Logger.logLine("Fitness is: " + fitness);
            count++;
        }

        Logger.logLine("XCSAgent learning phase done. Running 10 games with visuals.");

        options.setVisualization(true);
        
        // Run agent
        for (int i = 0; i < 10; i++)
        {
            int nextSeed = randomNumber.nextInt(Integer.MAX_VALUE);
            difficulty = i % 2;
            options.setLevelRandSeed(nextSeed);
            options.setLevelDifficulty(difficulty);
            learningTask.doEpisodes(1, true, 1);
        }
        // // Test
        // int[] seeds = IO.LoadObject("seeds");
        //
        // Logger.logLine("Difficulty 0");
        //
        // for (int i = 0; i < 500; i++)
        // {
        // options.setLevelDifficulty(0);
        // options.setLevelRandSeed(seeds[i]);
        // options.setVisualization(false);
        // learningTask.runSingleEpisodeWithFitness(sov);
        // }
        //
        // Logger.logLine("Difficulty 1");
        //
        // for (int i = 500; i < 1000; i++)
        // {
        // options.setLevelDifficulty(1);
        // options.setLevelRandSeed(seeds[i]);
        // options.setVisualization(false);
        // learningTask.runSingleEpisodeWithFitness(sov);
        // }

        Logger.logLine("Exiting program...");
        System.exit(0);
    }
}